#include<bits/stdc++.h>
#define ll long long
#define L x<<1
#define R x<<1|1
#define mid (l+r>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define OK Ll<=l&&r<=Rr
#define Root 1,1,n
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define repn(x) rep(x,1,n)
#define repm(x) rep(x,1,m)
#define pb push_back
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned ll
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(int x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
using namespace std;
const int N=1e2+5,M=1e6+5,inf=(1LL<<31)-1,mod=998244353;
const ll llf=1e18;
inline void add(int &a,int b){((a+=b)>=mod) and (a-=mod);}
inline int Add(int a,int b){return add(a,b),a;}
inline int mul(int a,int b){return 1LL*a*b%mod;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline void red(int &a,int b){add(a,mod-b);}
inline int Red(int a,int b){return red(a,b),a;}
inline int qp(int a,int b){if(!b)return 1;int c=qp(a,b>>1);Mul(c,c);if(b&1)Mul(c,a);return c;}
inline int INV(int x){return qp(x,mod-2);}
int n,k,a[N],lsh[N],b[N],ln,c[N],m,d[N],w[N],ct1[M];
inline int Id(int k){
	int ans=0;
	rep(i,1,k)ans+=d[i]*w[i];
	return ans;
}
ll f[M],F[M];
inline void subtask1(){
	//k很小,状态是每个划分的siz
	w[1]=1;
	rep(i,2,k)w[i]=w[i-1]*(m+1);//siz+1 进制
	rep(i,1,k)d[i]=m;
	int Mx=Id(k);
	rep(i,0,Mx)f[i]=F[i]=llf;
	f[0]=0;
	int tot=0,S=(1<<k)-1;
	rep(i,1,S)ct1[i]=ct1[i-(i&-i)]+1;
	rep(i,1,ln){
		rep(s,0,S)if(ct1[s]==c[i]){
			rep(c1,0,m-(s&1))rep(c2,0,m-((s>>1)&1))rep(c3,0,k<3?0:(m-((s>>2)&1)))rep(c4,0,k<4?0:(m-((s>>3)&1))){
				d[1]=c1,d[2]=c2,d[3]=c3,d[4]=c4;
				int j=Id(k);
				ll ex=0;
				rep(l,1,k)if((s>>l-1)&1){
					if(!d[l])ex-=b[i];
					else if(d[l]==m-1)ex+=b[i];
					d[l]++;
				}
				int t=Id(k);
				F[t]=min(F[t],f[j]+ex);
			}
		}
		rep(j,0,Mx)f[j]=F[j],F[j]=llf;
	}
	cout <<f[Mx];
}
inline void sol(int x,ll &ex){
	d[x]--,d[x+1]++;
	if(x==1)ex--;
	if(x==m)ex++;
}
inline void Sol(int x,ll &ex){
	d[x]++,d[x+1]--;
	if(x==1)ex++;
	if(x==m)ex--;
}
inline void subtask2(){
	//m=n/k 很小，状态是siz=i的划分个数
	w[1]=1;
	rep(i,2,m)w[i]=w[i-1]*(k+1);
	d[m]=k;
	int Mx=Id(m);
	rep(i,0,Mx)f[i]=F[i]=llf;
	f[k]=0;
	rep(i,1,ln){
		rep(c0,0,k)rep(c1,0,k-c0)rep(c2,0,m>2?k-c0-c1:0)rep(c3,0,m>3?k-c0-c1-c2:0){
			d[1]=c0,d[2]=c1,d[3]=c2,d[4]=c3;
			int j=Id(m);
			ll ex=0;
			rep(d1,0,d[1])rep(d2,0,min(c[i]-d1,d[2]))rep(d3,0,min(c[i]-d2-d1,d[3]))rep(d4,0,min(c[i]-d1-d2-d3,d[4])){
				ex=-d1;
				if(m==2)ex+=d2;
				else if(m==3)ex+=d3;
				else if(m==4)ex+=d4;
				d[1]-=d1,d[2]+=d1-d2,d[3]+=d2-d3,d[4]+=d3-d4;
				int t=Id(m);
				F[t]=min(F[t],f[j]+ex*b[i]);
				d[1]=c0,d[2]=c1,d[3]=c2,d[4]=c3;
			}
		}
		rep(j,0,Mx)f[j]=F[j],F[j]=llf;
	}
	cout <<f[0];
}
inline void Main(){
	n=read(),k=read(),m=n/k;
	repn(i)a[i]=read(),lsh[i]=a[i];
	sort(a+1,a+n+1),sort(lsh+1,lsh+n+1);
	lsh[n+1]=-1;
	repn(i)if(lsh[i]!=lsh[i+1])b[++ln]=lsh[i];
	repn(i)a[i]=lower_bound(b+1,b+ln+1,a[i])-b,c[a[i]]++;
	rep(i,1,ln)if(c[i]>k)return puts("-1"),void();
	if(k==1)return cout <<a[n]-a[1],void();
	if(m==1)return cout <<0,void();
	if(k<=4)return subtask1(),void();
	if(m<=4)return subtask2(),void();
}
signed main(){
	freopen("diyiti.in","r",stdin);
	freopen("diyiti.out","w",stdout);
	int T=1;
	while(T--)Main();
	return 0;
}	
